package org.fnzn.service.Impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.fnzn.bean.FnglHouse;
import org.fnzn.bean.FnglStaff;
import org.fnzn.bean.vo.User;
import org.fnzn.core.CommonConstant;
import org.fnzn.core.Constants;
import org.fnzn.core.Page;
import org.fnzn.core.Result;
import org.fnzn.dao.FnglHouseMapper;
import org.fnzn.dao.FnglStaffMapper;
import org.fnzn.service.FnglHouseService;
import org.fnzn.service.FnglStaffService;
import org.fnzn.service.base.AbstractService;
import org.fnzn.util.CommonUtil;
import org.fnzn.util.ResultGenerator;
import org.fnzn.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Condition;
import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.entity.Example.Criteria;

import javax.swing.plaf.ListUI;
import java.util.Date;
import java.util.List;

@Service
public class FnglHouseServiceImpl extends AbstractService<FnglHouse> implements FnglHouseService {

    @Autowired(required = false)
    private FnglHouseMapper fnglHouseMapper;
    @Autowired
    private FnglStaffService fnglStaffService;
    @Autowired(required = false)
    private FnglStaffMapper fnglStaffMapper;
    @Override
    public Result findFnglHouse(FnglHouse fnglHouse, Page page) {
        if(page.getLimit()!=null&& page.getPage()!=0){
            Condition condition = new Condition(FnglHouse.class);
            Criteria criteria=condition.createCriteria();
            // 根据删除状态查询
            criteria.andEqualTo("delFlag",0);
            // 根据搜索框条件查询
            if(!StringUtil.isEmpty(fnglHouse.getRealName())){
                criteria.andLike("realName","%"+fnglHouse.getRealName()+"%");
            }
            if(!StringUtil.isEmpty(fnglHouse.getCommunityName())){
                criteria.andLike("communityName","%"+fnglHouse.getCommunityName()+"%");
            }
            // 根据传来的房源类型
            if(!StringUtil.isEmpty(fnglHouse.getType())){
                criteria.andEqualTo("type",fnglHouse.getType());
            }
            if(!StringUtil.isEmpty(fnglHouse.getOfferPrice())){
                if(fnglHouse.getOfferPrice().contains("-")){
                    String[] offerPrice = fnglHouse.getOfferPrice().split("-");
                    criteria.andBetween("offerPrice",Integer.valueOf(offerPrice[0]),Integer.valueOf(offerPrice[1]));
                }else{
                    criteria.andGreaterThanOrEqualTo("offerPrice",Integer.valueOf(fnglHouse.getOfferPrice()));
                }
            }
            if(!StringUtil.isEmpty(fnglHouse.getArea())){
                if(fnglHouse.getArea().contains("-")){
                    String[] area = fnglHouse.getArea().split("-");
                    criteria.andBetween("area",Integer.valueOf(area[0]),Integer.valueOf(area[1]));
                }else{
                    criteria.andGreaterThanOrEqualTo("area",Integer.valueOf(fnglHouse.getArea()));
                }
            }
            // 根据当前登录用户所属二手房项目获取到对应的二手房源2
//            User user = CommonUtil.getUser();
//            FnglStaff fnglStaff = fnglStaffService.findBy("phone", user.getUser_id());
//            // 如果是超级管理员登录
//            if(user.getUser_id().equals(CommonConstant.USER_ID)){
//
//            }else{
//                // 根据当前登录人员所属项目id获取到对应项目下的二手房源
//                criteria.andEqualTo("projectId",fnglStaff.getProjectId());
//            }
            condition.setOrderByClause("create_time desc");
            PageHelper.startPage(page.getPage(),page.getLimit(), true);
            List<FnglHouse> list = findByCondition(condition);
            page.setRows(list);
            page.setTotal(new PageInfo(list).getTotal());
            return ResultGenerator.genSuccessResult(page);
        }
        return ResultGenerator.genFailResult("查询失败");
    }

    @Override
    public Result insertFnglHouse(FnglHouse fnglHouse) {
        if(fnglHouse!=null){
            fnglHouse.setHouseId(StringUtil.getUUID());
        }else{
            return ResultGenerator.genFailResult("房源为空");
        }
        // 保存销售人员
        Condition condition1 = new Condition(FnglStaff.class);
        Example.Criteria criteria1=condition1.createCriteria();
        criteria1.andEqualTo("delFlag",0);
        List<FnglStaff> fnglStaffs = fnglStaffService.findByCondition(condition1);
        fnglHouse.setRealName(fnglStaffs.get(0).getRealName());
        // 获取到传来的小区名称和房号和所属项目，如果已有相同数据，提示重新录入
        String communityName = fnglHouse.getCommunityName();
        String roomNumber = fnglHouse.getRoomNumber();
        // 获取到房源集合
        Condition condition = new Condition(FnglHouse.class);
        Example.Criteria criteria=condition.createCriteria();
        criteria.andEqualTo("delFlag",0);
        List<FnglHouse> fnglHouses = findByCondition(condition);
        if(fnglHouses!=null&&fnglHouses.size()>0){
            // 遍历集合
            for(FnglHouse item : fnglHouses){
                if(item.getCommunityName().equals(communityName)&&item.getRoomNumber().equals(roomNumber)&&item.getProjectId().equals(fnglHouse.getProjectId())){
                    return ResultGenerator.genFailResult("同一个门店下不能录入相同的房源!");
                }
            }
        }
        int n = save(fnglHouse);
        if(n>0){
            return ResultGenerator.genSuccessResult(fnglHouse);
        }else{
            return ResultGenerator.genFailResult("添加失败");
        }
    }

    @Override
    public Result updateFnglHouse(FnglHouse fnglHouse) {
        fnglHouse.setUpdateTime(new Date());
        // 保存销售人员
        FnglStaff fnglStaff = fnglStaffService.findBy("staffId", fnglHouse.getStaffId());
        fnglHouse.setRealName(fnglStaff.getRealName());
        int n = update(fnglHouse);
        if(n>0){
            return ResultGenerator.genSuccessResult(fnglHouse);
        }else{
            return ResultGenerator.genFailResult("更新失败");
        }
    }

    @Override
    public Result deleteFnglHouse(String houseId) {

        boolean flag = fnglHouseMapper.deleteFnglHouse(houseId);
        if(flag){
            return ResultGenerator.genSuccessResult("删除成功");
        }else{
            return ResultGenerator.genFailResult("删除失败");
        }
    }

    @Override
    public Result deleteAllFnglHouse(String Ids) {
        if(!StringUtil.isEmpty(Ids)){

            String[] HouseArr = Ids.split(",");
            int n = 0;
            for(String item : HouseArr){
                boolean flag = fnglHouseMapper.deleteFnglHouse(item);
                if(flag){
                    n+=1;
                }
            }
            if(n == HouseArr.length){
                return ResultGenerator.genSuccessResult("删除成功");
            }else{
                return ResultGenerator.genFailResult("删除失败");
            }

        }else{
            return ResultGenerator.genFailResult("删除失败");
        }
    }

    @Override
    public String[] getHouseImg(String houseId) {

        // 根据房源id获取到该房源详情
        FnglHouse fnglHouse = findById(houseId);
        if(fnglHouse == null){
            return null;
        }
        // 定义新数组
        String[] newImgs = new String[1];
        // 获取到图片
        String imgs = fnglHouse.getImg();
        if(!StringUtil.isEmpty(imgs)){
            if(imgs.contains(",")){
                newImgs = imgs.split(",");
            }else{
                newImgs[0] = imgs;
            }
            return newImgs;
        }else{
            return null;
        }
    }

    @Override
    public List<FnglHouse> getHouse(String projectId) {

        Condition condition = new Condition(FnglHouse.class);
        Criteria criteria=condition.createCriteria();
        criteria.andEqualTo("delFlag",0);
        criteria.andEqualTo("projectId",projectId);
        List<FnglHouse> list = findByCondition(condition);

        // 拼接小区名称+房号
        for(FnglHouse item : list){
            String name = item.getCommunityName()+""+item.getRoomNumber();
            item.setCommunityName(name);
        }

        return list;
    }
}

